package me.xiaosiqi.web.controller.system;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import me.xiaosiqi.domain.system.Role;
import me.xiaosiqi.utils.BeanUtil;
import me.xiaosiqi.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author xiaosiqi
 * @date 2021/6/3 18:01
 */

@WebServlet("/system/role")
public class RoleServlet extends BaseServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/javascript;charset=utf-8");
        String operation = req.getParameter("operation");
        System.out.println(operation);
        if ("list".equals(operation)){
            // uri:/system/role?operation=list
            list(req,resp);
        }else if ("delete".equals(operation)){
            // uri:/system/role?operation=delete
            delete(req,resp);
        }else if ("toAdd".equals(operation)){
            toAdd(req,resp);
        }else if ("save".equals(operation)){
            save(req,resp);
        }else if ("toEdit".equals(operation)){
            toEdit(req,resp);
        }else if ("edit".equals(operation)){
            edit(req,resp);
        }else if ("author".equals(operation)){
            author(req,resp);
        }else if ("updateRoleModule".equals(operation)){
            updateRoleModule(req,resp);
        }
    }

    private void updateRoleModule(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String[] moduleIds = req.getParameter("moduleIds").split(",");
        String roleId= req.getParameter("roleId");
        moduleService.updateRoleModule(roleId,moduleIds);
        list(req,resp);
    }

    private void author(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Role role = roleService.findById(id);
        req.setAttribute("role",role);
        List<Map>  roleModule = moduleService.findAuthorDataByRoleId(id);
        ObjectMapper om=new ObjectMapper();
        String json = om.writeValueAsString(roleModule);
        req.setAttribute("roleModuleJson",json);
        //跳转页面
        req.getRequestDispatcher("/WEB-INF/pages/system/role/author.jsp").forward(req,resp);
    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Role role = BeanUtil.fillBean(req, Role.class);
        roleService.update(role);
        resp.sendRedirect(req.getContextPath()+"/system/role?operation=list");
    }

    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Role role = roleService.findById(id);
        List<Role> roleList = roleService.findAll();
        req.setAttribute("roleList",roleList);
        req.setAttribute("role",role);
        //跳转页面
        req.getRequestDispatcher("/WEB-INF/pages/system/role/update.jsp").forward(req,resp);
    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Role role= BeanUtil.fillBean(req,Role.class);
        roleService.save(role);
        resp.sendRedirect(req.getContextPath()+"/system/role?operation=list");
    }

    private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Role> roleList = roleService.findAll();
        req.setAttribute("roleList",roleList);
        //跳转页面
        req.getRequestDispatcher("/WEB-INF/pages/system/role/add.jsp").forward(req,resp);

    }


    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Role role = new Role();
        role.setId(id);
        roleService.delete(role);
        resp.sendRedirect(req.getContextPath()+"/system/role?operation=list");

    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int page = 1;
        int size = 15;
       if (StringUtils.isNoneBlank(req.getParameter("page"))){
           page = Integer.parseInt(req.getParameter("page"));
       }
        if (StringUtils.isNoneBlank(req.getParameter("size"))){
            size = Integer.parseInt(req.getParameter("size"));
        }
        PageInfo pageInfo = roleService.findPage(page, size);
        req.setAttribute("page",pageInfo);
        req.getRequestDispatcher("/WEB-INF/pages/system/role/list.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
